home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / intuition / io_methods / CloseWindowSafely.c next >
C/C++ Source or Header  |  1992-09-03  |  3KB  |  97 lines

  1.  
  2. /*
  3. Copyright (c) 1992 Commodore-Amiga, Inc.
  4.  
  5. This example is provided in electronic form by Commodore-Amiga, Inc. for
  6. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  7. published by Addison-Wesley (ISBN 0-201-56774-1).
  8.  
  9. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  10. information on the correct usage of the techniques and operating system
  11. functions presented in these examples.  The source and executable code
  12. of these examples may only be distributed in free electronic form, via
  13. bulletin board or as part of a fully non-commercial and freely
  14. redistributable diskette.  Both the source and executable code (including
  15. comments) must be included, without modification, in any copy.  This
  16. example may not be published in printed form or distributed with any
  17. commercial product.  However, the programming techniques and support
  18. routines set forth in these examples may be used in the development
  19. of original executable software products for Commodore Amiga computers.
  20.  
  21. All other rights reserved.
  22.  
  23. This example is provided "as-is" and is subject to change; no
  24. warranties are made.  All use is at your own risk. No liability or
  25. responsibility is assumed.
  26. */
  27.  
  28. /* CloseWindowSafely
  29. **
  30. ** these functions close an Intuition window that shares a port with other
  31. ** Intuition windows.
  32. **
  33. ** We are careful to set the UserPort to NULL before closing, and to free
  34. ** any messages that it might have been sent.
  35. */
  36. #include "exec/types.h"
  37. #include "exec/nodes.h"
  38. #include "exec/lists.h"
  39. #include "exec/ports.h"
  40. #include "intuition/intuition.h"
  41.  
  42. /*
  43. ** function to remove and reply all IntuiMessages on a port that have been
  44. ** sent to a particular window (note that we don't rely on the ln_Succ pointer
  45. ** of a message after we have replied it)
  46. */
  47. VOID StripIntuiMessages(struct MsgPort *mp, struct Window *win)
  48. {
  49. struct IntuiMessage *msg;
  50. struct Node *succ;
  51.  
  52. msg = (struct IntuiMessage *)mp->mp_MsgList.lh_Head;
  53.  
  54. while (succ = msg->ExecMessage.mn_Node.ln_Succ)
  55.         {
  56.         if (msg->IDCMPWindow == win)
  57.                 {
  58.                 /* Intuition is about to free this message.
  59.                 ** Make sure that we have politely sent it back.
  60.                 */
  61.                 Remove(msg);
  62.  
  63.                 ReplyMsg(msg);
  64.                 }
  65.         msg = (struct IntuiMessage *)succ;
  66.         }
  67. }
  68.  
  69. /*
  70. ** Entry point to CloseWindowSafely()
  71. ** Strip all IntuiMessages from an IDCMP which are waiting for a specific
  72. ** window.  When the messages are gone, set the UserPort of the window to NULL
  73. ** and call ModifyIDCMP(win,0).  This will free the Intuition arts of the
  74. ** IDMCMP and trun off message to this port without changing the original
  75. ** UserPort (which may be in use by other windows).
  76. */
  77. VOID CloseWindowSafely(struct Window *win)
  78. {
  79. /* we forbid here to keep out of race conditions with Intuition */
  80. Forbid();
  81.  
  82. /* send back any messages for this window  that have not yet been processed */
  83. StripIntuiMessages(win->UserPort, win);
  84.  
  85. /* clear UserPort so Intuition will not free it */
  86. win->UserPort = NULL;
  87.  
  88. /* tell Intuition to stop sending more messages */
  89. ModifyIDCMP(win, 0L);
  90.  
  91. /* turn multitasking back on */
  92. Permit();
  93.  
  94. /* Now it's safe to really close the window */
  95. CloseWindow(win);
  96. }
  97.